package com.example.demo01.entity.dto;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.example.demo01.config.EmpExcel;
import com.example.demo01.entity.Emp;

import java.util.ArrayList;
import java.util.List;

public class ImportEmpDTO extends AnalysisEventListener<EmpExcel> {

    private final List<Emp> empList = new ArrayList<>();

    @Override
    public void invoke(EmpExcel data, AnalysisContext context) {
        Emp emp = new Emp();
        emp.setName(data.getName());
        emp.setGender("男".equals(data.getGender()) ? (byte) 1 :(byte) 2);
        Integer jobId = getJobId(data.getJob());
        emp.setJob(jobId != null ? jobId.byteValue() : null);
        emp.setEntrydate(java.time.LocalDate.parse(data.getEntrydate()));
        emp.setImage(data.getImage());
        emp.setUsername(data.getUsername());
        emp.setPassword("123456"); // 默认密码
        emp.setCreateTime(java.time.LocalDateTime.now());
        emp.setUpdateTime(java.time.LocalDateTime.now());
        empList.add(emp);
    }

    private Integer getJobId(String jobName) {
        return switch (jobName) {
            case "班主任" -> 1;
            case "讲师" -> 2;
            case "学工主管" -> 3;
            case "教研主管" -> 4;
            default -> null;
        };
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {}

    public List<Emp> getEmpList() {
        return empList;
    }
}
